package com.iweb.education.teacher.dao;

import com.iweb.education.Utils.JdbcUtil;
import com.iweb.education.teacher.model.Teacher;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherDaoImp implements TeacherDao{

    @Override
    public Teacher selectByUsernamePassword(String teachername, String teacherpassword) {
        String sql = "select teacherid,teachername,teacherpassword,teachercourse,teachersex,teacherphone from teacher where teachername =? and teacherpassword =?";
        List<Map<String,Object>> list =JdbcUtil.queryBySql(sql,teachername,teacherpassword);
        if(list.size()>0){
            Map<String,Object> map = list.get(0);
            Integer teacherid = (Integer) map.get("teacherid");
            String teachercourse = (String) map.get("teachercourse");
            String teachersex = (String) map.get("teachersex");
            String teacherphone = (String) map.get("teacherphone");
            Teacher teacher = new Teacher(teacherid,teachername,teacherpassword,teachercourse,teachersex,teacherphone);
            return teacher;
        }
        return null;
    }
    @Override
    public int getTotalStudentCount() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int count = 0;

        try {
            conn = JdbcUtil.getCollection();
            String sql = "SELECT COUNT(*) FROM student";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.closeSource();
        }

        return count;
    }

    @Override
    public List<Student> getStudentsByPage(int start, int pageSize) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Student> students = new ArrayList<>();

        try {
            conn = JdbcUtil.getCollection();
            String sql = "SELECT * FROM student LIMIT ?, ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, start);
            pstmt.setInt(2, pageSize);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Student student = new Student();
                student.setSid(rs.getInt("sid"));
                student.setSname(rs.getString("sname"));
                student.setSsex(rs.getString("ssex"));
                student.setSphone(rs.getString("sphone"));
                student.setSage(rs.getInt("sage"));
                student.setSadress(rs.getString("sadress"));
                // 注意：不要返回密码字段
                students.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeSource();
        }
        System.out.println("dao:"+students);
        return students;
    }
    @Override
    public Student getStudentById(int sid) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Student student = null;

        try {
            conn = JdbcUtil.getCollection();
            String sql = "SELECT * FROM student WHERE sid = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, sid);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                student = new Student();
                student.setSid(rs.getInt("sid"));
                student.setSname(rs.getString("sname"));
                student.setSsex(rs.getString("ssex"));
                student.setSphone(rs.getString("sphone"));
                student.setSage(rs.getInt("sage"));
                student.setSadress(rs.getString("sadress"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeSource();
        }

        return student;
    }

    @Override
    public boolean updateStudent(Student student) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            conn = JdbcUtil.getCollection();
            String sql = "UPDATE student SET sname=?,ssex=?, sphone=?, sage=?, sadress=? WHERE sid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,student.getSname());
            pstmt.setString(2, student.getSsex());
            pstmt.setString(3, student.getSphone());
            pstmt.setInt(4, student.getSage());
            pstmt.setString(5, student.getSadress());
            pstmt.setInt(6, student.getSid());

            int rows = pstmt.executeUpdate();
            success = rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeSource();
        }

        return success;
    }

    @Override
    public boolean deleteStudent(int sid) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            conn = JdbcUtil.getCollection();
            String sql = "DELETE FROM student WHERE sid=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, sid);

            int rows = pstmt.executeUpdate();
            success = rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeSource();
        }

        return success;
    }
}

